package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.Post;
import com.example.entity.PostApplication;
import com.example.entity.Student;
import com.example.service.PostApplicationService;
import com.example.service.PostService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("postApplications")
public class PostApplicationController {

    @Autowired
    private PostApplicationService postApplicationService;

    @Autowired
    private PostService postService;


    /**
     * 岗位申请分页查询
     * @return
     */
    @GetMapping("/getPagePostApplication/{current}/{size}/{code}")
    public R getPagePostApplication(@PathVariable int current,@PathVariable int size,@PathVariable int code,PostApplication postApplication){
        IPage page =new Page(current,size);
        LambdaQueryWrapper<PostApplication> lqw =new LambdaQueryWrapper<>();


        if (code==1){
            lqw.like(Strings.isNotEmpty(postApplication.getJobName()),PostApplication::getJobName,postApplication.getJobName());
            lqw.eq(Strings.isNotEmpty(postApplication.getCommercialId()),PostApplication::getCommercialId,postApplication.getCommercialId());

            //在岗学生（statusApplication为true的—--在岗学生调用这个接口会传statusApplication过来）
            lqw.eq(Strings.isNotEmpty(postApplication.getStatusApplication()),PostApplication::getStatusApplication,postApplication.getStatusApplication());

            IPage page1 = postApplicationService.page(page,lqw);

            return R.success("求职者审核||在岗学生分页查询！",page1);
        }else if (code==2){
            lqw.eq(PostApplication::getStuNumber,postApplication.getStuNumber());

            IPage page1 = postApplicationService.page(page,lqw);

            return R.success("学生查看个人录用结果分页查询！",page1);
        }else if (code==3){
            lqw.eq(PostApplication::getStuNumber,postApplication.getStuNumber());
            lqw.eq(PostApplication::getStatusApplication,"true");

            IPage page1 = postApplicationService.page(page,lqw);

            return R.success("学生查看个人工资详情分页查询！",page1);
        }

        return R.error("岗位信息查询失败!");
    }

    /**
     * 添加岗位申请
     * @return
     */
    @PostMapping("/addPostApplication")
    public R addPostApplication(@RequestBody PostApplication postApplication){

        LambdaQueryWrapper<PostApplication> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(postApplication.getStuNumber()),PostApplication::getStuNumber,postApplication.getStuNumber());
        lqw.eq(Strings.isNotEmpty(postApplication.getJobName()),PostApplication::getJobName,postApplication.getJobName());
        lqw.eq(Strings.isNotEmpty(postApplication.getJobTime()),PostApplication::getJobTime,postApplication.getJobTime());

        //1.查询数据库此申请是否存在
        PostApplication one = postApplicationService.getOne(lqw);

        //2.不存在在添加
        if (one==null){
            boolean save = postApplicationService.save(postApplication);
            if (save){
                return R.success("添加岗位申请成功！",null);
            }else {
                return R.error("添加岗位申请失败！");
            }
        }else {
            //3.存在
            return R.error("已申请！无须在申请！");
        }
    }

    /**
     * 通过id查询岗位申请
     * @return
     */
    @GetMapping("/getPostApplicationById")
    public R getPostApplicationById(@PathVariable int id){

        LambdaQueryWrapper<PostApplication> lqw =new LambdaQueryWrapper<>();
        lqw.eq(PostApplication::getId,id);
        PostApplication one = postApplicationService.getOne(lqw);
        return R.success("通过id查询岗位申请",one);
    }

    /**
     * 通过id修改岗位申请
     * @return
     */
    @PutMapping("/updatePostApplication")
    public R updatePostApplication(@RequestBody PostApplication postApplication){

        boolean b = postApplicationService.updateById(postApplication);
        if (b){
           return R.success("通过id修改岗位申请成功",postApplication);
        }else {
           return R.error("通过id修改岗位申请失败！");
        }
    }

    /**
     * 通过id删除岗位申请
     * @return
     */
    @DeleteMapping("/deletePostApplication/{id}")
    public R deletePostApplication(@PathVariable int id){

        boolean b = postApplicationService.removeById(id);
        if (b){
           return R.success("通过id删除岗位申请成功",id);
        }else {
           return R.error("通过id删除岗位申请失败！");
        }
    }

    /**
     * 根据id查询岗位申请信息
     * @return
     */
    @GetMapping("/getMyself/{id}")
    public R getMyself(@PathVariable int id){

        PostApplication one = postApplicationService.getById(id);
        return R.success("根据id查询岗位申请信息(修改回显)",one);
    }

    /**
     * 通过id修改学生岗位申请状态
     * @return
     */
    @PutMapping("/updatePostApplicationStatus")
    public R updatePostApplicationStatus(@RequestBody PostApplication postApplication){


        boolean b = postApplicationService.updateById(postApplication);
        if (b){
            return R.success("通过id修改学生岗位申请状态成功",postApplication);
        }else {
            return R.error("通过id修改学生岗位申请状态失败！");
        }
    }


}

